package com.zmn.plat.business.impl.price.meter3;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.business.impl.price.PriceUtil;
import com.zmn.plat.business.interfaces.brand.ratio.BrandRatioBService;
import com.zmn.plat.business.interfaces.channel.ChannelServiceCategoryBService;
import com.zmn.plat.business.interfaces.pilot.ChannelPriceBService;
import com.zmn.plat.business.interfaces.price.meter3.MeterFlow3CommonBService;
import com.zmn.plat.business.interfaces.product.ServProductBService;
import com.zmn.plat.business.interfaces.servcategory.ShowCategoryAssociationBService;
import com.zmn.plat.common.constant.ItemCategoryConsts;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.common.dictionary.BaseCityPriceTypeEnum;
import com.zmn.plat.common.dictionary.BaseCityRatioTypeEnum;
import com.zmn.plat.common.dictionary.ChannelCityRatioTypeEnum;
import com.zmn.plat.common.dto.brand.factor.BrandFactorDRO;
import com.zmn.plat.common.dto.price.RatioTo;
import com.zmn.plat.common.dto.price.meter3.ItemDRO3;
import com.zmn.plat.common.dto.price.meter3.ProductTariffTypeDRO;
import com.zmn.plat.common.dto.price.meter3.ProductTariffTypeQuery;
import com.zmn.plat.common.enums.PilotConfigEnum;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.manager.dictionary.ProductDict;
import com.zmn.plat.model.dto.price.PriceCalDTO;
import com.zmn.plat.model.dto.product.ProductRelationDTO;
import com.zmn.plat.model.entity.base.city.ratio.BaseCityRatio;
import com.zmn.plat.model.entity.base.city.ratio.BaseCityRatioQuery;
import com.zmn.plat.model.entity.brand.BrandRatio;
import com.zmn.plat.model.entity.brand.BrandRatioQuery;
import com.zmn.plat.model.entity.channel.ChannelCityPriceRatio;
import com.zmn.plat.model.entity.channel.ChannelCityPriceRatioQuery;
import com.zmn.plat.model.entity.product.ServProduct;
import com.zmn.plat.model.entity.product.tariff.ProductTariffQuery;
import com.zmn.plat.model.entity.product.tariff.item.ProductTariffGroupItemQuery;
import com.zmn.plat.model.entity.servcategory.ShowCategoryAssociationVO;
import com.zmn.plat.model.vo.product.tariff.ProductTariffVO;
import com.zmn.plat.model.vo.service.tariff.ChannelServiceTariffVO;
import com.zmn.plat.services.interfaces.base.city.ratio.BaseCityRatioService;
import com.zmn.plat.services.interfaces.brand.BrandRatioService;
import com.zmn.plat.services.interfaces.channel.ChannelCityPriceRatioService;
import com.zmn.plat.services.interfaces.channel.brand.ratio.ChannelBrandRatioService;
import com.zmn.plat.services.interfaces.channel.tariff.ChannelTariffService;
import com.zmn.plat.services.interfaces.product.ServProductService;
import com.zmn.plat.services.interfaces.product.tariff.ProductTariffService;
import com.zmn.plat.services.interfaces.product.tariff.item.ProductTariffGroupItemService;
import com.zmn.plat.services.interfaces.product.tariff.tariffproductitem3.TariffProductItem3Service;
import com.zmn.plat.services.interfaces.service.tariff.ServiceTariffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 描述: 计价器3.0公用实现类
 *
 * @author guoqiao
 * @since 2021/11/19 10:38
 */
@Service
@Slf4j
public class MeterFlow3CommonBServiceImpl implements MeterFlow3CommonBService {

    // 默认系数
    private final static Float RESULT_RATIO = 1.0F;
    // 系数不存在缓存值给定值
    private final static Float CACHE_DEFAULT_RATIO = -1.0F;
    @Resource
    ChannelPriceBService channelPriceBService;
    @Resource
    ServProductService servProductService;
    @Resource
    ServProductBService servProductBService;
    @Resource
    ChannelTariffService channelTariffService;
    @Resource
    ChannelServiceCategoryBService channelServiceCategoryBService;
    @Resource
    ShowCategoryAssociationBService showCategoryAssociationBService;
    @Resource
    ServiceTariffService serviceTariffService;
    @Resource
    ProductTariffService productTariffService;
    @Resource
    ProductTariffGroupItemService productTariffGroupItemService;
    @Resource
    MeterFlow3CommonBService meterFlow3CommonBService;
    @Resource
    TariffProductItem3Service tariffProductItem3Service;
    @Resource
    ChannelCityPriceRatioService channelCityPriceRatioService;
    @Resource
    BaseCityRatioService baseCityRatioService;
    @Resource
    ChannelBrandRatioService channelBrandRatioService;
    @Resource
    BrandRatioService brandRatioService;
    @Resource
    BrandRatioBService brandRatioBService;

    // 本缓存时间
    private static final int MAX_CACHE_EXP = 6;
    // 缓存前缀（区分非计价器3.0）
    private static final String RATIO_PREFIX = "price3_";
    // 渠道折扣本地缓存
    private Cache<String, Float> channelCache = CacheBuilder.newBuilder().expireAfterWrite(MAX_CACHE_EXP, TimeUnit.HOURS).build();
    // 城市系数本地缓存
    private Cache<String, Float> cityCache = CacheBuilder.newBuilder().expireAfterWrite(MAX_CACHE_EXP, TimeUnit.HOURS).build();
    // 品牌系数本地缓存
    private Cache<String, Float> brandCache = CacheBuilder.newBuilder().expireAfterWrite(MAX_CACHE_EXP, TimeUnit.HOURS).build();

    @Override
    @Cacheable(cacheNames = "redis10m", key = "'price3:getProductTariffType:channelId:'+#p0+':productId:'+#p1+':bizType:'+#p2+':tariffType:'+#p3", unless = "#result == null")
    public ProductTariffTypeDRO getProductTariffType(Integer channelId, Integer productId, Integer bizType, Integer tariffType) {

        Integer avaliableChannelId = channelId;
        try {
            avaliableChannelId = channelServiceCategoryBService.getAvailableCategoryChannelId(channelId);
        } catch (PlatException e) {}
        // 查询渠道价格表
        ChannelServiceTariffVO channelTariff = channelTariffService.findFaultTariffByChannelId(avaliableChannelId, bizType, tariffType);

        // 查询符合条件的价格表id集合 serv_product_tariff
        List<Integer> tariffIds = Optional.ofNullable(serviceTariffService.listAll())
                .orElse(Collections.emptyList())
                .stream()
                .filter(e -> Objects.equals(bizType, e.getBizType())
                        && Objects.equals(tariffType, e.getTariffType())
                        && Objects.equals(GlobalConsts.YES, e.getStatus()))
                .map(e -> e.getTariffId())
                .collect(Collectors.toList());

        // 查询产品价格表 serv_product_tariff
        ProductTariffQuery productTariffQuery = new ProductTariffQuery();
        productTariffQuery.setProductId(productId);
        productTariffQuery.setStatus(GlobalConsts.YES);
        List<ProductTariffVO> productTariffList = Optional.ofNullable(productTariffService.listByQueryWithCache(productTariffQuery))
                .orElse(Collections.emptyList())
                .stream()
                .filter(e -> tariffIds.contains(e.getTariffId()))
                .collect(Collectors.toList());

        int count = 0;
        // 检查渠道价格表是否可用
        if (Objects.nonNull(channelTariff) && NumberUtil.isNotNullOrZero(channelTariff.getTariffId())) {
            List<Integer> tariffIdList = productTariffList.stream().map(e -> e.getTariffId()).collect(Collectors.toList());
            if (tariffIdList.contains(channelTariff.getTariffId())) {
                // 判断产品在当前价格表下是否可以用 serv_product_tariff_group_item
                ProductTariffGroupItemQuery itemQuery = new ProductTariffGroupItemQuery();
                itemQuery.setProductId(productId);
                itemQuery.setTariffId(channelTariff.getTariffId());
                if (Objects.equals(ProductDict.TRIFF_TYPE_TARIFF3, tariffType)) {
                    itemQuery.setCategId(ItemCategoryConsts.MAINTAIN_ITEM);
                    count = tariffProductItem3Service.countByQuery(itemQuery);
                } else {
                    count = productTariffGroupItemService.countByQuery(itemQuery);
                }
                if (count > 0) {
                    return new ProductTariffTypeDRO(productId, channelTariff.getTariffId(), channelTariff.getTariffType());
                }
            }
        }

        // 产品默认价格表id
        List<ProductTariffVO> defaultTariffList = productTariffList.stream()
                .filter(e -> Objects.equals(GlobalConsts.YES, e.getGeneral()))
                .collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(defaultTariffList)) {
            // 判断产品在当前价格表下是否可以用 serv_product_tariff_group_item
            ProductTariffGroupItemQuery itemQuery = new ProductTariffGroupItemQuery();
            itemQuery.setProductId(productId);
            itemQuery.setTariffId(defaultTariffList.get(0).getTariffId());
            if (Objects.equals(ProductDict.TRIFF_TYPE_TARIFF3, tariffType)) {
                itemQuery.setCategId(ItemCategoryConsts.MAINTAIN_ITEM);
                count = tariffProductItem3Service.countByQuery(itemQuery);
            } else {
                count = productTariffGroupItemService.countByQuery(itemQuery);
            }
            if (count > 0) {
                return new ProductTariffTypeDRO(productId, defaultTariffList.get(0).getTariffId(), defaultTariffList.get(0).getTariffType());
            }
        }
        return null;
    }

    @Override
    @Cacheable(cacheNames = "redis10m", key = "'price3:listProductTariffTypeByQuery:ProductTariffTypeQuery:'+#p0", unless = "#result == null")
    public List<ProductTariffTypeDRO> listProductTariffTypeByQuery(ProductTariffTypeQuery query) {

        Integer channelId = query.getChannelId();
        Integer cityId = query.getCityId();
        Integer bizType = query.getBizType();
        Integer showType = query.getShowType();
        Integer maxTariffType = query.getMaxTariffType();
        Integer avaliableChannelId = channelId;
        try {
            avaliableChannelId = channelServiceCategoryBService.getAvailableCategoryChannelId(channelId);
        } catch (PlatException e) {
            log.error("无可用渠道");
        }

        List<ProductTariffTypeDRO> droList = new ArrayList<>(query.getProductIdList().size());

        for (Integer productId : query.getProductIdList()) {
            boolean flag = false; // 该productId是否已明确价格表，用于跳出循环
            // 查询产品
            ServProduct product = servProductService.findByKeyWithCache(productId);
            if (Objects.isNull(product)) {
                droList.add(new ProductTariffTypeDRO(productId, null, null));
                log.error("产品不存在，id{}", productId);
                continue;
            }
            if (NumberUtil.isNullOrZero(query.getShowType())) {
                showType = product.getShowType();
            }
            List<ProductRelationDTO> collect = Optional.ofNullable
                    (servProductBService.listProductRalationByIdListAndShowType(Arrays.asList(productId), showType))
                    .orElse(Collections.emptyList())
                    .stream()
                    .filter(e -> {
                        ServProduct erpProduct = servProductService.findByKeyWithCache(e.getProductId());
                        if (Objects.nonNull(erpProduct) && erpProduct.getBizType().contains(String.valueOf(bizType))) {
                            return true;
                        }
                        return false;
                    }).collect(Collectors.toList());

            // 后台产品id集合（用作查询价格）
            List<Integer> erpProductIdList = collect.stream().map(e -> e.getProductId()).distinct().collect(Collectors.toList());
            // 计价器价格表需要过滤计价器状态
            List<Integer> erpProductIdListFilterStatus = collect.stream()
                    // 计价器价格表需要过滤计价器状态
                    .filter(e -> Objects.equals(e.getTariffStatus(), GlobalConsts.YES) && Objects.equals(e.getTariffProductStatus(), GlobalConsts.YES))
                    .map(e -> e.getProductId())
                    .distinct()
                    .collect(Collectors.toList());

            Integer servCategId = product.getServCategId();
            Integer categId = product.getCategId();
            // 如果是后端产品，转换成前端产品分类
            if (Objects.equals(showType, ProductConsts.ERP_PRODUCT_TYPE)) {
                List<ShowCategoryAssociationVO> showCategoryAssociationVOS =
                        showCategoryAssociationBService.listByCategoryId(product.getServCategId(), product.getCategOneId(), product.getCategId());
                if (!CollectionUtils.isEmpty(showCategoryAssociationVOS)) {
                    servCategId = showCategoryAssociationVOS.get(0).getShowServCategId();
                    categId = showCategoryAssociationVOS.get(0).getShowCategId();
                }
            }

            if (NumberUtil.isNullOrZero(maxTariffType) || maxTariffType >= ProductDict.TRIFF_TYPE_TARIFF3) {
                // 计价器3.0试点
                Boolean traflag3 = channelPriceBService.checkByChannelIdAndCityIdAndTypeAndCateg(channelId, cityId, PilotConfigEnum.NEW_FEE_METER.getType(), servCategId, categId);
                if (traflag3) {
                    // 查询计价器3.0价格
                    flag = this.getTariff(droList, erpProductIdList, avaliableChannelId, bizType, ProductDict.TRIFF_TYPE_TARIFF3, productId);
                    if (flag) {
                        continue;
                    }
                }
            }

            // 计价器试点
            Boolean traflag = channelPriceBService.checkByChannelIdAndCityIdAndTypeAndCateg(channelId, cityId, PilotConfigEnum.FEE_METER.getType(), servCategId, categId);
            if (traflag) {
                // 是否支持提前付试点
                Boolean advflag = channelPriceBService.checkByChannelIdAndCityIdAndTypeAndCateg(channelId, cityId, PilotConfigEnum.PAY_ADVANCE.getType(), servCategId, categId);
                if (advflag) {
                    // 查询提前付价格
                    flag = this.getTariff(droList, erpProductIdList, avaliableChannelId, bizType, ProductDict.TRIFF_TYPE_ADVANCE, productId);
                    if (flag) {
                        continue;
                    }
                }
                // 查询计价器价格
                flag = this.getTariff(droList, erpProductIdListFilterStatus, avaliableChannelId, bizType, ProductDict.TRIFF_TYPE_TARIFF, productId);
                if (flag) {
                    continue;
                }
            }

            // 查询服务价格
            flag = this.getTariff(droList, erpProductIdList, avaliableChannelId, bizType, ProductDict.TRIFF_TYPE_SERVICE, productId);
            if (flag) {
                continue;
            }
            droList.add(new ProductTariffTypeDRO(productId, null, null));
        }
        return droList;
    }

    @Override
    public void calPriceDetail(ItemDRO3 itemDRO3, PriceCalDTO calDTO) {
        if (!Objects.equals(itemDRO3.getPriceStandard(), GlobalConsts.YES)) {
            log.info("calPriceDetail非一口价不计算价格，项目id:{}", itemDRO3.getItemId());
            return;
        }
        // 获取系数（渠道折扣，城市系数，品牌系数，分为内外部不同类型）
        RatioTo ratioTo = this.fillRatio(itemDRO3, calDTO);
        log.info("系数To为：{}", ratioTo);
        // 计算价格
        this.calculatePrice(itemDRO3, ratioTo, calDTO.getBizType());
    }

    /**
     * 描述: 1.将价格信息放进dro中；2.如已设置了价格表信息：返回true 否则返回false
     *
     * @return java.lang.Boolean
     * @author guoqiao
     * @since 2021/11/19 18:35
     */
    private Boolean getTariff(List<ProductTariffTypeDRO> droList, List<Integer> erpProductIdList, Integer channel, Integer bizType, Integer tariffType, Integer productId) {
        for (Integer erpProductId : erpProductIdList) {
            ProductTariffTypeDRO productTariffType = meterFlow3CommonBService.getProductTariffType(channel, erpProductId, bizType, tariffType);
            if (Objects.nonNull(productTariffType)) {
                droList.add(new ProductTariffTypeDRO(productId, productTariffType.getTariffId(), productTariffType.getTariffType()));
                return true;
            }
        }
        return false;
    }

    /**
     * 获取系数（渠道、品牌、城市）
     */
    private RatioTo fillRatio(ItemDRO3 itemDRO3, PriceCalDTO calDTO) {
        Integer channelId = calDTO.getChannelId();
        Integer cityId = calDTO.getCityId();
        Integer brandId = calDTO.getBrandId();
        Integer bizType = calDTO.getBizType();
        Integer productId = itemDRO3.getProductId();

        RatioTo ratioTo = new RatioTo();
        ratioTo.setItemId(itemDRO3.getItemId());
        ratioTo.setProductId(productId);
        // 渠道折扣
        if (NumberUtil.isNotNullOrZero(channelId)) {
            Float channelDiscont = getChannelDiscount(channelId, bizType, BaseCityPriceTypeEnum.PRICE.getCode());
            Float internalChannelDiscont = getChannelDiscount(channelId, bizType, BaseCityPriceTypeEnum.INNERPRICE.getCode());
            ratioTo.setChannelDiscont(channelDiscont);
            ratioTo.setInternalChannelDiscont(internalChannelDiscont);
        }
        // 获取品牌系数
        if (NumberUtil.isNotNullOrZero(brandId)) {
            Float brandRatio = getBrandRatio(channelId, brandId, productId, BaseCityPriceTypeEnum.PRICE.getCode());
            Float internalBrandRatio = getBrandRatio(channelId, brandId, productId, BaseCityPriceTypeEnum.INNERPRICE.getCode());
            ratioTo.setBrandRatio(brandRatio);
            ratioTo.setInternalBrandRatio(internalBrandRatio);
        }
        // 城市系数
        if (NumberUtil.isNotNullOrZero(cityId)) {
            Float cityRatio = getCityRatio(channelId, cityId, BaseCityPriceTypeEnum.PRICE.getCode());
            Float internalCityRatio = getCityRatio(channelId, cityId, BaseCityPriceTypeEnum.INNERPRICE.getCode());
            ratioTo.setCityRatio(cityRatio);
            ratioTo.setInternalCityRatio(internalCityRatio);
        }
        return ratioTo;
    }

    /**
     * 描述: 获取渠道系数
     */
    private Float getChannelDiscount(Integer channelId, Integer bizType, Integer priceType) {
        try {
            String key = channelId + "_" + bizType + "_" + priceType;
            return channelCache.get(key, () -> {
                ChannelCityPriceRatioQuery channelCityPriceRatioQuery = new ChannelCityPriceRatioQuery();
                channelCityPriceRatioQuery.setChannelId(channelId);
                channelCityPriceRatioQuery.setBizType(bizType);
                channelCityPriceRatioQuery.setPriceType(priceType);
                channelCityPriceRatioQuery.setRatioType(ChannelCityRatioTypeEnum.DISCOUNTRATIO.getCode());
                ChannelCityPriceRatio channelCityPriceRatio = channelCityPriceRatioService.findByChannelIdAndCityAndCategAndType(channelCityPriceRatioQuery);
                if (Objects.nonNull(channelCityPriceRatio) && NumberUtil.isNotNullOrZero(channelCityPriceRatio.getRatio())) {
                    return new BigDecimal(channelCityPriceRatio.getRatio()).floatValue();
                }
                return CACHE_DEFAULT_RATIO;
            });
        } catch (Exception e) {
            log.error("缓存方式获取渠道折扣失败:{},", e);
            throw new PlatException("缓存方式获取渠道折扣失败");
        }
    }

    /**
     * 描述: 获取品牌系数
     */
    private Float getBrandRatio(Integer channelId, Integer brandId, Integer productId, Integer brandPriceType) {
        try {
            String key = RATIO_PREFIX + channelId + "_" + brandId + "_" + productId + "_" + brandPriceType;
            return brandCache.get(key, () -> {
                // 查询渠道品牌系数
//                ChannelBrandRatioQuery channelBrandQuery = new ChannelBrandRatioQuery();
//                channelBrandQuery.setChannelId(channelId);
//                channelBrandQuery.setBrandId(brandId);
//                channelBrandQuery.setRatioType(ChannelBrandRatioTypeEnum.BRANDMETER3RATIO.getCode());
//                channelBrandQuery.setPriceType(brandPriceType);
//                List<ChannelBrandRatio> channelRatioList = channelBrandRatioService.listByQuery(channelBrandQuery);
//                if (!CollectionUtils.isEmpty(channelRatioList)) {
//                    Double channelRatio = channelRatioList.get(0).getRatio();
//                    if (NumberUtil.isNotNullOrZero(channelRatio)) {
//                        return new Float(channelRatio).floatValue();
//                    }
//                }

                // 查询基础品牌计价器3.0分类系数
                BrandFactorDRO brandFactorDRO = brandRatioBService.getByBrandIdAndProductIdAndBizTypeAndPriceType(brandId, productId, 4, brandPriceType);
                if (Objects.nonNull(brandFactorDRO) && NumberUtil.isNotNullOrZero(brandFactorDRO.getRatio())) {
                    return new BigDecimal(brandFactorDRO.getRatio()).floatValue();
                }

                // 查询基础品牌计价器3.0默认系数
                BrandRatioQuery baseQuery = new BrandRatioQuery();
                baseQuery.setBrandId(brandId);
                baseQuery.setRatioType(BaseCityRatioTypeEnum.CITYMETER3RATIO.getCode());
                baseQuery.setPriceType(brandPriceType);
                List<BrandRatio> baseRatioList = brandRatioService.listByQuery(baseQuery);
                if (!CollectionUtils.isEmpty(baseRatioList)) {
                    Double baseRatio = baseRatioList.get(0).getRatio();
                    if (NumberUtil.isNotNullOrZero(baseRatio)) {
                        return new Float(baseRatio).floatValue();
                    }
                }
                return CACHE_DEFAULT_RATIO;
            });
        } catch (Exception e) {
            log.error("缓存方式获取品牌系数失败:{},", e);
            throw new PlatException("缓存方式获取品牌系数失败");
        }
    }

    /**
     * 描述: 获取城市系数
     */
    private Float getCityRatio(Integer channelId, Integer cityId, Integer cityPriceType) {
        try {
            String key = RATIO_PREFIX + channelId + "_" + cityId + "_" + cityPriceType;
            return cityCache.get(key, () -> {
                // 查询渠道城市系数
                ChannelCityPriceRatioQuery channelCityQuery = new ChannelCityPriceRatioQuery();
                channelCityQuery.setChannelId(channelId);
                channelCityQuery.setCityId(cityId);
                channelCityQuery.setRatioType(ChannelCityRatioTypeEnum.CHANNELCITYMETER3RATIO.getCode());
                channelCityQuery.setPriceType(cityPriceType);
                List<ChannelCityPriceRatio> channelRatioList = channelCityPriceRatioService.listByQuery(channelCityQuery);
                if (!CollectionUtils.isEmpty(channelRatioList)) {
                    Double channelRatio = channelRatioList.get(0).getRatio();
                    if (NumberUtil.isNotNullOrZero(channelRatio)) {
                        return new Float(channelRatio).floatValue();
                    }
                }
                // 查询基础城市系数
                BaseCityRatioQuery baseQuery = new BaseCityRatioQuery();
                baseQuery.setCityId(cityId);
                baseQuery.setRatioType(BaseCityRatioTypeEnum.CITYMETER3RATIO.getCode());
                baseQuery.setPriceType(cityPriceType);
                List<BaseCityRatio> baseRatioList = baseCityRatioService.listByQuery(baseQuery);
                if (!CollectionUtils.isEmpty(baseRatioList)) {
                    Double baseRatio = baseRatioList.get(0).getRatio();
                    if (NumberUtil.isNotNullOrZero(baseRatio)) {
                        return new Float(baseRatio).floatValue();
                    }
                }
                return CACHE_DEFAULT_RATIO;
            });
        } catch (Exception e) {
            log.error("缓存方式获取城市系数失败:{},", e);
            throw new PlatException("缓存方式获取城市系数失败");
        }
    }

    /**
     * 描述: 计算配件费/路途往返费/技术工时费/内部价
     */
    private void calculatePrice(ItemDRO3 itemDRO3, RatioTo ratioTo, Integer bizType) {
        /*********** 计算配件费 ***********/
        Integer partPrice = itemDRO3.getPartPrice();
        // 渠道折扣
        if (Objects.nonNull(itemDRO3.getPartFeeImpactfactor()) && itemDRO3.getPartFeeImpactfactor().contains("2")) {
            partPrice = PriceUtil.getPriceBy689(partPrice, ratioTo.getChannelDiscont(), bizType);
        }
        // 城市系数
        if (Objects.equals(itemDRO3.getCityLimit(), GlobalConsts.YES) && Objects.nonNull(itemDRO3.getPartFeeImpactfactor()) && itemDRO3.getPartFeeImpactfactor().contains("3")) {
            partPrice = PriceUtil.getPriceBy689(partPrice, ratioTo.getCityRatio(), bizType);
        }
        // 品牌系数
        if (Objects.equals(itemDRO3.getBrandLimit(), GlobalConsts.YES) && Objects.nonNull(itemDRO3.getPartFeeImpactfactor()) && itemDRO3.getPartFeeImpactfactor().contains("1")) {
            partPrice = PriceUtil.getPriceBy689(partPrice, ratioTo.getBrandRatio(), bizType);
        }
        itemDRO3.setPartPrice(partPrice);

        /*********** 计算路途往返费 ***********/
        Integer trafficFee = itemDRO3.getTrafficFee();
        // 渠道折扣
        if (Objects.nonNull(itemDRO3.getTrafficFeeImpactfactor()) && itemDRO3.getTrafficFeeImpactfactor().contains("2")) {
            trafficFee = PriceUtil.getPriceBy689(trafficFee, ratioTo.getChannelDiscont(), bizType);
        }
        // 城市系数
        if (Objects.equals(itemDRO3.getCityLimit(), GlobalConsts.YES) && Objects.nonNull(itemDRO3.getTrafficFeeImpactfactor()) && itemDRO3.getTrafficFeeImpactfactor().contains("3")) {
            trafficFee = PriceUtil.getPriceBy689(trafficFee, ratioTo.getCityRatio(), bizType);
        }
        // 品牌系数
        if (Objects.equals(itemDRO3.getBrandLimit(), GlobalConsts.YES) && Objects.nonNull(itemDRO3.getTrafficFeeImpactfactor()) && itemDRO3.getTrafficFeeImpactfactor().contains("1")) {
            trafficFee = PriceUtil.getPriceBy689(trafficFee, ratioTo.getBrandRatio(), bizType);
        }
        itemDRO3.setTrafficFee(trafficFee);

        /*********** 计算技术工时费 ***********/
        Integer hourFee = itemDRO3.getHourFee();
        // 渠道折扣
        if (Objects.nonNull(itemDRO3.getHourFeeImpactfactor()) && itemDRO3.getHourFeeImpactfactor().contains("2")) {
            hourFee = PriceUtil.getPriceBy689(hourFee, ratioTo.getChannelDiscont(), bizType);
        }
        // 城市系数
        if (Objects.equals(itemDRO3.getCityLimit(), GlobalConsts.YES) && Objects.nonNull(itemDRO3.getHourFeeImpactfactor()) && itemDRO3.getHourFeeImpactfactor().contains("3")) {
            hourFee = PriceUtil.getPriceBy689(hourFee, ratioTo.getCityRatio(), bizType);
        }
        // 品牌系数
        if (Objects.equals(itemDRO3.getBrandLimit(), GlobalConsts.YES) && Objects.nonNull(itemDRO3.getHourFeeImpactfactor()) && itemDRO3.getHourFeeImpactfactor().contains("1")) {
            hourFee = PriceUtil.getPriceBy689(hourFee, ratioTo.getBrandRatio(), bizType);
        }
        itemDRO3.setHourFee(hourFee);
        itemDRO3.setPrice(partPrice + trafficFee + hourFee);

        /*********** 计算内部价 ***********/
        if (NumberUtil.isNullOrZero(itemDRO3.getInternalSettlementPrice())) {
            itemDRO3.setInternalSettlementPrice(partPrice + trafficFee + hourFee);
        } else {
            Integer internalSettlementPrice = itemDRO3.getInternalSettlementPrice();
            // 渠道折扣
            if (Objects.nonNull(itemDRO3.getInternalSettlementPriceImpactfactor()) && itemDRO3.getInternalSettlementPriceImpactfactor().contains("2")) {
                internalSettlementPrice = PriceUtil.getPriceBy689(internalSettlementPrice, ratioTo.getInternalChannelDiscont(), bizType);
            }
            // 城市系数
            if (Objects.equals(itemDRO3.getCityLimit(), GlobalConsts.YES) && Objects.nonNull(itemDRO3.getInternalSettlementPriceImpactfactor()) && itemDRO3.getInternalSettlementPriceImpactfactor().contains("3")) {
                internalSettlementPrice = PriceUtil.getPriceBy689(internalSettlementPrice, ratioTo.getInternalCityRatio(), bizType);
            }
            // 品牌系数
            if (Objects.equals(itemDRO3.getBrandLimit(), GlobalConsts.YES) && Objects.nonNull(itemDRO3.getInternalSettlementPriceImpactfactor()) && itemDRO3.getInternalSettlementPriceImpactfactor().contains("1")) {
                internalSettlementPrice = PriceUtil.getPriceBy689(internalSettlementPrice, ratioTo.getInternalBrandRatio(), bizType);
            }
            itemDRO3.setInternalSettlementPrice(internalSettlementPrice);

        }
        // 外部价赋值
        if (NumberUtil.isNullOrZero(itemDRO3.getExternalSettlementPrice())) {
            itemDRO3.setExternalSettlementPrice(partPrice + trafficFee + hourFee);
        }
    }


}
